package com.google.bling.client.widget;

import com.google.bling.client.Util;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PerUserAmountWidget extends Composite {
  public interface Listener {
    void onChanged();
  }

  private final FlowPanel panel = new FlowPanel();

  private final Label label;

  private final TextBox amountBox = new TextBox();

  private final TextBox percentBox = new TextBox();

  private int amount;

  private int percent;

  private int totalAmount;

  private Listener listener;

  public PerUserAmountWidget(String userName, int totalAmount,
      int initialAmount, int initialPercent) {
    this.totalAmount = totalAmount;
    initWidget(panel);
    label = new Label(userName);
    panel.add(label);
    panel.add(amountBox);
    panel.add(percentBox);
    amountBox.setText("" + initialAmount);
    percentBox.setText("" + initialPercent);

    blockify(label);
    blockify(amountBox);
    blockify(percentBox);

    amountBox.addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        onAmountChanged();
      }
    });
    amountBox.addKeyboardListener(new KeyboardListenerAdapter() {
      public void onKeyUp(Widget sender, char keyCode, int modifiers) {
        onAmountChanged();
      }
    });
    percentBox.addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        onPercentChanged();
      }
    });
    percentBox.addKeyboardListener(new KeyboardListenerAdapter() {
      @Override
      public void onKeyUp(Widget sender, char keyCode, int modifiers) {
        onPercentChanged();
      }
    });
  }

  private void blockify(Widget w) {
    w.addStyleName("amount-box");
  }

  protected void onAmountChanged() {
//    if (!Util.isNumber(amountBox.getText())) {
//      return;
//    }
    amount = Util.parseInt(amountBox.getText());
    percent = ((amount * 100) / totalAmount);
    percentBox.setText("" + percent);
    onChanged();
  }

  protected void onPercentChanged() {
//    if (!Util.isNumber(percentBox.getText())) {
//      return;
//    }
    percent = Util.parseInt(percentBox.getText());
    amount = (percent * totalAmount) / 100;
    amountBox.setText("" + amount);
    onChanged();
  }

  private void onChanged() {
    if (listener != null) {
      listener.onChanged();
    }
  }

  public String getUserName() {
    return label.getText();
  }

  public int getAmount() {
    return amount;
  }

  public int getPercent() {
    return percent;
  }

  public void clearInput() {
    amountBox.setText("");
    percentBox.setText("");
  }

  public void setTotalAmount(int totalAmount) {
    this.totalAmount = totalAmount;
    onPercentChanged();
  }

  public void setListener(Listener listener) {
    this.listener = listener;
  }
}
